package net.sk.china.city.service.impl;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import net.sk.china.city.domain.dto.donate.DonateSourceDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.sk.china.city.utils.DateUtils;
import net.sk.china.city.utils.SecurityUtils;
import net.sk.china.common.exception.ServiceException;
import net.sk.china.city.utils.PlatformIdHolder;
import net.sk.china.city.service.IArDonateSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.sk.china.city.mapper.ArDonateSourceMapper;
import net.sk.china.city.domain.ArDonateSource;

/**
 * 捐赠来源Service业务层处理
 * 
 * @author gxf
 * @date 2024-04-24
 */
@Service
public class ArDonateSourceServiceImpl implements IArDonateSourceService
{
    @Autowired
    private ArDonateSourceMapper arDonateSourceMapper;

    /**
     * 查询捐赠来源
     * 
     * @param id 捐赠来源主键
     * @return 捐赠来源
     */
    @Override
    public ArDonateSource selectArDonateSourceById(Long id)
    {
        return arDonateSourceMapper.selectArDonateSourceById(id);
    }

    /**
     * 查询捐赠来源列表
     * 
     * @param arDonateSource 捐赠来源
     * @return 捐赠来源
     */
    @Override
    public List<ArDonateSource> selectArDonateSourceList(ArDonateSource arDonateSource)
    {
        arDonateSource.setPlatformId(PlatformIdHolder.getPlatformId());
        return arDonateSourceMapper.selectArDonateSourceList(arDonateSource);
    }

    /**
     * 新增捐赠来源
     * 
     * @param arDonateSource 捐赠来源
     * @return 结果
     */
    @Override
    public int insertArDonateSource(ArDonateSource arDonateSource)
    {
        ArDonateSource donateSource = arDonateSourceMapper.selectOne(new QueryWrapper<ArDonateSource>()
                .eq("donate_name", arDonateSource.getDonateName()));
        if(donateSource != null){
            throw new ServiceException("该捐赠人已存在");
        }
        arDonateSource.setPlatformId(PlatformIdHolder.getPlatformId());
        arDonateSource.setCreateTime(LocalDate.now());
        arDonateSource.setCreateBy(SecurityUtils.getUsername());
        return arDonateSourceMapper.insert(arDonateSource);
    }

    /**
     * 修改捐赠来源
     * 
     * @param arDonateSource 捐赠来源
     * @return 结果
     */
    @Override
    public int updateArDonateSource(ArDonateSource arDonateSource)
    {
        ArDonateSource donateSource = arDonateSourceMapper.selectOne(new QueryWrapper<ArDonateSource>()
                .eq("id", arDonateSource.getId()));
        if(donateSource !=null && arDonateSource.getDonateName() != null && !donateSource.getDonateName().equals(arDonateSource.getDonateName())){
            ArDonateSource source = arDonateSourceMapper.selectOne(new QueryWrapper<ArDonateSource>()
                    .ne("id", arDonateSource.getId())
                    .eq("donate_name",arDonateSource.getDonateName())
                    .like("platform_id","%" + PlatformIdHolder.getPlatformId() +"%"));
            if(source != null){
                throw new ServiceException("该捐赠人已存在");
            }
        }
        arDonateSource.setUpdateTime(LocalDate.now());
        arDonateSource.setUpdateBy(SecurityUtils.getUsername());
        return arDonateSourceMapper.updateArDonateSource(arDonateSource);
    }

    /**
     * 批量删除捐赠来源
     * 
     * @param ids 需要删除的捐赠来源主键
     * @return 结果
     */
    @Override
    public int deleteArDonateSourceByIds(Long[] ids)
    {
        return arDonateSourceMapper.deleteArDonateSourceByIds(ids);
    }

    /**
     * 删除捐赠来源信息
     * 
     * @param id 捐赠来源主键
     * @return 结果
     */
    @Override
    public int deleteArDonateSourceById(Long id)
    {
        return arDonateSourceMapper.deleteArDonateSourceById(id);
    }

    @Override
    public List<DonateSourceDTO> donateList(){
        List<DonateSourceDTO> donateSourceDTOS =  new ArrayList<>();
        QueryWrapper<ArDonateSource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",'0');
        queryWrapper.like("platform_id","%" + PlatformIdHolder.getPlatformId() +"%");
        List<ArDonateSource> arDonateSources = arDonateSourceMapper.selectList(queryWrapper);
        for (ArDonateSource arDonateSource : arDonateSources){
            DonateSourceDTO donateSourceDTO = new DonateSourceDTO();
            donateSourceDTO.setId(arDonateSource.getId());
            donateSourceDTO.setDonateName(arDonateSource.getDonateName());
            donateSourceDTOS.add(donateSourceDTO);
        }
        return donateSourceDTOS;
    }
}
